package com.teatrace.service.storage.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.teatrace.dao.BaseDao;
import com.teatrace.dao.template.DaoCallBack;
import com.teatrace.dao.template.DaoTemplate;
import com.teatrace.dao.template.impl.AutoTransactionDaoTemplate;
import com.teatrace.service.storage.StorageService;


public class StorageServiceImpl implements StorageService {
	
	private DaoTemplate getAutoTransactionDaoTemplate() {
		return new AutoTransactionDaoTemplate();
	}

	public void add(final Object object) throws Exception {
		// TODO Auto-generated method stub
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				dao.save(object);
				return null;
			}
		});
	}

	public void deleteById(final Class<?> clazz, final Serializable pk) throws Exception {
		// TODO Auto-generated method stub
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				dao.delete(dao.load(clazz, pk));
				return null;
			}
		});
		
	}

	public <T> Object getById(final Class<T> clazz, final Serializable pk) throws Exception {
		// TODO Auto-generated method stub
	return this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				Object obj = dao.get(clazz, pk);
				return obj;
			}
		});
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> listByCriteria(final Class<T> clazz, final int page,
			final String parameter, final Object value) throws Exception {
		// TODO Auto-generated method stub
	return  (List<T>) this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				
				DetachedCriteria query = DetachedCriteria.forClass(
						clazz).add(Restrictions.eq(parameter, value));
				List list = dao.listByCriteria(query, (page - 1) * 10, 10);
				return list;
			}
		});
	}

	public <T> List<T> listByPage(Class<T> clazz, int page) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public <T> Object loadById(final Class<T> clazz, final Serializable pk)
			throws Exception {
		// TODO Auto-generated method stub
		return this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				Object obj = dao.load(clazz, pk);
				return obj;
			}
		});
	}

	public void update(final Object obj) throws Exception {
		// TODO Auto-generated method stub
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				dao.update(obj);
				return null;
			}
		});
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> listByCriteria(final Class<T> clazz,final int page, final String pk,
			final Object pkValue, final String parameter, final Object value) throws Exception {
		// TODO Auto-generated method stub
		return (List<T>) this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				DetachedCriteria criteria = dao.createDetachedCriteria(clazz);
				criteria.add(Restrictions.eq(pk, pkValue)).add(Restrictions.eq(parameter, value));
				List list = dao.listByCriteria(criteria,(page - 1) * 10, 10);
				return list;
			}
		});
	}

}
